package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.model.domain.ServeProvider;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.WorkerCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.CertificationAuditReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.RejectReasonResDTO;
import com.jzo2o.customer.service.IServeProviderService;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.customer.service.WorkerCertificationAuditService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Service
public class WorkerCertificationAuditServiceImpl extends ServiceImpl<WorkerCertificationAuditMapper,
        WorkerCertificationAudit> implements WorkerCertificationAuditService {

    @Resource
    IWorkerCertificationService workerCertificationService;

    @Resource
    IServeProviderService serveProviderService;



    @Override
    public void certification(WorkerCertificationAuditAddReqDTO workerCertificationAuditAddReqDTO) {


        WorkerCertificationAudit workerCertificationAudit =
                BeanUtil.toBean(workerCertificationAuditAddReqDTO, WorkerCertificationAudit.class);

        workerCertificationAudit.setAuditStatus(0);

        baseMapper.insert(workerCertificationAudit);

        Long serveProviderId = workerCertificationAuditAddReqDTO.getServeProviderId();
        WorkerCertification workerCertification = workerCertificationService.getById(serveProviderId);
        if(ObjectUtil.isNotNull(workerCertification)){

            workerCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
            workerCertificationService.updateById(workerCertification);
        }else{

            workerCertification = new WorkerCertification();
            workerCertification.setId(serveProviderId);
            workerCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
            workerCertificationService.save(workerCertification);
        }


    }

    @Override
    public RejectReasonResDTO rejectReason() {

        Long userId = UserContext.currentUserId();

        WorkerCertificationAudit workerCertificationAudit = baseMapper.selectOne(
                new QueryWrapper<WorkerCertificationAudit>()
                        .lambda().eq(WorkerCertificationAudit::getServeProviderId, userId)
        );
        RejectReasonResDTO rejectReasonResDTO = new RejectReasonResDTO();
        rejectReasonResDTO.setRejectReason(workerCertificationAudit.getRejectReason());

        return  rejectReasonResDTO;
    }

    @Override
    public PageResult<WorkerCertificationAudit> pageList(WorkerCertificationAuditPageQueryReqDTO workerCertificationAuditPageQueryReqDTO) {

        Page<WorkerCertificationAudit> workerCertificationAuditPage =
                PageUtils.parsePageQuery(workerCertificationAuditPageQueryReqDTO, WorkerCertificationAudit.class);


        Page<WorkerCertificationAudit> pageList = baseMapper.selectPage(workerCertificationAuditPage, null);

        return PageUtils.toPage(pageList,WorkerCertificationAudit.class);
    }


    //认证信息审核状态
    @Transactional
    @Override
    public void auditCertification(Long id, CertificationAuditReqDTO certificationAuditReqDTO) {

        CurrentUserInfo currentUserInfo = UserContext.currentUser();

        LambdaUpdateWrapper<WorkerCertificationAudit> set = new UpdateWrapper<WorkerCertificationAudit>().lambda()
                .eq(WorkerCertificationAudit::getId, id) //修改id为 ..
                .set(WorkerCertificationAudit::getAuditorName, currentUserInfo.getName()) //审核人名称
                .set(WorkerCertificationAudit::getAuditStatus, 1)//已审核
                .set(WorkerCertificationAudit::getAuditorId, currentUserInfo.getId())//审核人id
                .set(WorkerCertificationAudit::getAuditTime, LocalDateTime.now()) //审核时间
                .set(WorkerCertificationAudit::getRejectReason,certificationAuditReqDTO.getRejectReason())
                .set(WorkerCertificationAudit::getCertificationStatus, certificationAuditReqDTO.getCertificationStatus());
        super.update(set);


        WorkerCertificationAudit workerCertificationAudit = baseMapper.selectById(id);

        if(certificationAuditReqDTO.getCertificationStatus().intValue() == CertificationStatusEnum.SUCCESS.getStatus()){


            WorkerCertification workerCertification1 = BeanUtil.toBean(workerCertificationAudit, WorkerCertification.class);
            workerCertification1.setId(workerCertificationAudit.getServeProviderId());
            workerCertification1.setCertificationTime(workerCertificationAudit.getAuditTime());
            workerCertificationService.updateById(workerCertification1);
        }else {
            WorkerCertification workerCertification = new WorkerCertification();
            workerCertification.setId(workerCertificationAudit.getServeProviderId());
            workerCertification.setCertificationStatus(certificationAuditReqDTO.getCertificationStatus());
            workerCertification.setCertificationTime(workerCertificationAudit.getAuditTime());
            workerCertificationService.updateById(workerCertification);
        }

    }

}
